package thread;

import junit.framework.TestCase;

import java.util.concurrent.*;

/**
 * @author zhangxin
 * @date 2020/10/13
 */
public class ThreadPoolTest extends TestCase {
    ExecutorService executorService = Executors.newCachedThreadPool();

    public void testWait() throws InterruptedException {
        Object lock = new Object();
        lock.wait();
    }

    public void testStop() throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(2000L);
                Thread innnerThread = new Thread(() -> {
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
                innnerThread.start();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        thread.stop();

        Thread.sleep(4000);
    }

    public void test() throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < 10; i++) {
            int finalI = i;
            executorService.execute(() -> {
                try {
                    Thread.sleep(1);
                    System.out.println(finalI);
                } catch (InterruptedException e) {
                }
            });
        }


        executorService.execute(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("t2");
            } catch (InterruptedException e) {
            }
        });


        executorService.execute(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            System.out.println("t3");
        });
        Thread.sleep(10000);
    }

    public void testPool() throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 1L, TimeUnit.MINUTES, new SynchronousQueue<>(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        //循环提交任务
        for (int i = 0; i < 500000; i++) {
            //提交任务的索引
            final int index = (i + 1);
            System.out.println(index);
            threadPoolExecutor.submit(() -> {
                //线程打印输出
                System.out.println("大家好，我是线程：" + Thread.currentThread().getName());
                try {
                    //模拟线程执行时间，10s
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                byte[] b2 = new byte[1024 * 1024];

                try {
                    Thread.sleep(3000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                byte[] b3 = new byte[1024 * 1024];


            });
            //每个任务提交后休眠500ms再提交下一个任务，用于保证提交顺序
            Thread.sleep(500);
        }

        Thread.sleep(500000);
    }
}
